package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.GwCustomerInfo;
import com.ruoyi.system.domain.GwEnterpriseInfo;
import com.ruoyi.system.domain.GwInvestorInfo;
import com.ruoyi.system.domain.GwStaff;
import com.ruoyi.system.domain.vo.HomePageStatVo;
import com.ruoyi.system.domain.vo.HomePageTodoVo;
import com.ruoyi.system.mapper.GwCustomerInfoMapper;
import com.ruoyi.system.mapper.GwHomePageMapper;
import com.ruoyi.system.mapper.GwInvestorInfoMapper;
import com.ruoyi.system.mapper.GwStaffMapper;
import com.ruoyi.system.service.IGwHomePageService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class GwHomePageServiceImpl implements IGwHomePageService {
    @Resource
    private GwHomePageMapper gwHomePageMapper;
    @Resource
    private GwCustomerInfoMapper gwCustomerInfoMapper;
    @Resource
    private GwStaffMapper gwStaffMapper;
    @Resource
    private GwInvestorInfoMapper gwInvestorInfoMapper;

    @Override
    public AjaxResult selectData() {
        AjaxResult ajax = new AjaxResult();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return AjaxResult.error(200, "未查询到用户登录信息,请先登录！");
        } else {
            String createBy = "";
            if(!"1".equals(loginUser.getUser().getUserType())){
                createBy = loginUser.getUser().getNickName();
            }
            /*List<String> userIdList = new ArrayList<>();
            GwInvestorInfo gwInvestorInfo = new GwInvestorInfo();
            gwInvestorInfo.setPhoneNumber(SecurityUtils.getUsername());
            List<GwInvestorInfo> gwInvestorInfos = gwInvestorInfoMapper.selectGwInvestorInfoList(gwInvestorInfo);
            if (ObjectUtils.isNotEmpty(gwInvestorInfos)) {
                userIdList.add(gwInvestorInfos.get(0).getId() + "");
            }
            GwStaff gwStaff = new GwStaff();
            gwStaff.setTel(SecurityUtils.getUsername());
            List<GwStaff> gwStaffs = gwStaffMapper.selectGwStaffList(gwStaff);
            if (ObjectUtils.isNotEmpty(gwStaffs)) {
                userIdList.add(gwStaffs.get(0).getId() + "");
            }
            GwCustomerInfo gwCustomerInfo = new GwCustomerInfo();
            gwCustomerInfo.setPhoneNumber(SecurityUtils.getUsername());
            List<GwCustomerInfo> gwCustomerInfos = gwCustomerInfoMapper.selectGwCustomerInfoList(gwCustomerInfo);
            if (ObjectUtils.isNotEmpty(gwCustomerInfos)) {
                userIdList.add(gwCustomerInfos.get(0).getId() + "");
            }*/
            String companyId = loginUser.getEnterpriseId();
            HomePageStatVo homePageStatVo = new HomePageStatVo();
            Integer progressContractCount = gwHomePageMapper.selectProgressContractCount(createBy, companyId);
            Integer acceptContractCount = gwHomePageMapper.selectAcceptContractCount(createBy, companyId);
            Integer remainingContractCount = 0;
            Integer remainingMessageCount = 0;
            if (StringUtils.isNotEmpty(companyId)) {
                remainingContractCount = gwHomePageMapper.selectRemainingContractCount(companyId);
                remainingMessageCount = gwHomePageMapper.selectRemainingMessageCount(companyId);
            }
            homePageStatVo.setProgressContractCount(progressContractCount);
            homePageStatVo.setAcceptContractCount(acceptContractCount);
            homePageStatVo.setRemainingContractCount(remainingContractCount);
            homePageStatVo.setRemainingMessageCount(remainingMessageCount);

            HomePageTodoVo homePageTodoVo = gwHomePageMapper.selectTodoContractCount(createBy, companyId);
            ajax.put("statistics", homePageStatVo);
            ajax.put("todo", homePageTodoVo);
            return ajax;
        }
    }
}
